package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.DestinationSearchQuery;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("destination")
public class DestinationController {
    @Reference
    private IRegionService regionService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyCatalogService catalogService;
    @Reference
    private IStrategyDetailService detailService;
    @Reference
    private ITravelService travelService;
    @Reference
    private IBigThemeService bigThemeService;
    @Reference
    private IDestinationThemeService destinationThemeService;
    @Reference
    private IMonthDestinationService monthDestinationService;
    @Reference
    private IDestIdDestThemeService destThemeService;

    @RequestMapping("")
    public String index(Model model){
        model.addAttribute("hotRegions", regionService.queryHotRegions());
        model.addAttribute("bigThemes",bigThemeService.list());
        //查询所有的月份
        List<MonthDestination> mons = monthDestinationService.selectAll();
        model.addAttribute("mons",mons);

        //根据大主题id查询
        model.addAttribute("yT",destinationThemeService.QueryDestinationThemeById(1L));
        model.addAttribute("seasonT",destinationThemeService.QueryDestinationThemeById(2L));
        model.addAttribute("tripT",destinationThemeService.QueryDestinationThemeById(3L));
        model.addAttribute("hoildayT",destinationThemeService.QueryDestinationThemeById(4L));

        return "destination/index";
    }


    //根据月份查询相应的目的地
    @RequestMapping("/getDestByMonId")
    public String getDestByMonId(Model model, Long mouth){

        //1:查询数据
        List<Destination> mdests = destinationService.getDestByMonId(mouth);

        model.addAttribute("dests",mdests );
        model.addAttribute("monthId",mouth);

        return "destination/mondestTpl";
    }

    //目的地筛选
    @RequestMapping("/destfilter")
    public String destfilter(Model model,@ModelAttribute("qo") DestinationSearchQuery qo){

        //查询所有的月份
        List<MonthDestination> mons = monthDestinationService.selectAll();
        model.addAttribute("mons",mons);

        //全年
        List<DestinationTheme> usuals = destinationThemeService.selectByParentId(DestinationTheme.ALL_YEAR);
        model.addAttribute("usuals",usuals);

        //季节
        List<DestinationTheme> seasons  = destinationThemeService.selectByParentId(DestinationTheme.SEASON);
        model.addAttribute("seasons",seasons);

        //出行方式
        List<DestinationTheme> modes = destinationThemeService.selectByParentId(DestinationTheme.MODE);
        model.addAttribute("modes",modes);

        //假期
        List<DestinationTheme> holidays = destinationThemeService.selectByParentId(DestinationTheme.HOLIDAY);
        model.addAttribute("holidays",holidays);
        PageInfo pageInfo = destThemeService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "/destination/destfilter";
    }


    @RequestMapping("/getHotDestByRegionId")
    public String getHotDestByRegionId(Model model, Long regionId){
        //1:查询数据
        List<Destination> list = destinationService.getDestByRegionId(regionId);
        //2:拆分数据
        int size = list.size();
        //左边:leftDests
        model.addAttribute("leftDests",list.subList(0, size / 2 + 1) );
        //右边:rightDests
        model.addAttribute("rightDests",list.subList(size / 2 + 1,size ));
        //3:拼接html模板
        model.addAttribute("regionId", regionId);
        return "destination/hotdestTpl";
    }

    @RequestMapping("/getDestinationThemeById")
    public String getDestinationThemeById(Model model, Long themeId){

        /*for (DestinationTheme destinationTheme : list) {
            System.err.println(destinationTheme);
        }
        model.addAttribute("list",list);
        //3:拼接html模板
        model.addAttribute("themeId", themeId);*/
        return "destination/destThemeTpl";
    }


    //查看某个目的地的明细
    @RequestMapping("/guide")
    public Object guide(Long id, Model model){
        //catalogs
        List<StrategyCatalog> catalogs = catalogService.queryByDestId(id);
        model.addAttribute("catalogs",catalogs);

        //toast
        List<Destination> toasts = destinationService.getToasts(id);
        //dest
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);

        //strategyDetails
        List<StrategyDetail> strategyDetails = detailService.queryViewNumTop3(id);
        model.addAttribute("strategyDetails",strategyDetails);
        return "destination/guide";
    }

    //(左边)查看目的地分类
    @RequestMapping("/surveyPage")
    public Object surveyPage(@ModelAttribute("qo") StrategyCatalogQuery qo, Model model){
        //toast
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());

        //dest
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);
        return "/destination/survey";
    }

    //(右边)查看目的地分类第一篇明细
    @RequestMapping("/survey")
    public Object survey(@ModelAttribute("qo") StrategyCatalogQuery qo,Model model){
        //catalogs
        List<StrategyCatalog> catalogs = catalogService.queryByDestId(qo.getDestId());
        model.addAttribute("catalogs",catalogs);

        //catalog
        StrategyCatalog catalog = null;
        for (StrategyCatalog c : catalogs) {
            if(c.getId() == qo.getCatalogId()){
                catalog = c;
                model.addAttribute("catalog",catalog);
            }
        }

        //detail
        if(catalog != null){
            List<StrategyDetail> details = catalog.getDetails();
            if(details != null && details.size() > 0){
                StrategyDetail detail = details.get(0);
                detail.setStrategyContent(detailService.getContentByDetailId(detail.getId()));
                model.addAttribute("detail",details.get(0));
            }
        }
        return "/destination/surveyTpl";
    }

    //目的地明细中游记列表
    @RequestMapping("/travels")
    public String travels(Model model, @ModelAttribute("qo") TravelQuery qo){
        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "/destination/travelTpl";
    }


}
